﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace System
{
    /// <summary>
    /// ObjectExtension
    /// </summary>
    public static partial class ObjectExtension
    {
        /// <summary>
        /// 转换成Json字符串
        /// </summary>
        /// <param name="this"></param>
        /// <returns></returns>
        public static string ToJson(this ExpandoObject @this)
        {
            return JsonConvert.SerializeObject(@this);
        }
        /// <summary>
        /// 转换成Json字符串
        /// </summary>
        /// <param name="this"></param>
        /// <returns></returns>
        public static string ToJson(this object @this)
        {
            if (@this == null)
            {
                return string.Empty;
            }
            return JsonConvert.SerializeObject(@this);
        }
        /// <summary>
        /// 反序列化为指定的对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static T FromJson<T>(this string @this)
        {
            return JsonConvert.DeserializeObject<T>(@this);
        }
        /// <summary>
        /// 类型转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static T To<T>(this object @this)
        {
            if (@this != null)
            {
                Type targetType = typeof(T);

                if (@this.GetType() == targetType)
                {
                    return (T)@this;
                }

                TypeConverter converter = TypeDescriptor.GetConverter(@this);
                if (converter.CanConvertTo(targetType))
                {
                    return (T) converter.ConvertTo(@this, targetType);
                }

                converter = TypeDescriptor.GetConverter(targetType);
                if (converter.CanConvertFrom(@this.GetType()))
                {
                    return (T)converter.ConvertFrom(@this);
                }
                if (@this == DBNull.Value)
                {
                    return (T)(object)null;
                }
            }
            return (T)@this;
        }
        /// <summary>
        /// 类型转换
        /// </summary>
        /// <param name="this"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object To(this object @this, Type type)
        {
            if (@this != null)
            {
                Type targetType = type;

                if (@this.GetType() == targetType)
                {
                    return @this;
                }

                TypeConverter converter = TypeDescriptor.GetConverter(@this);
                if (converter.CanConvertTo(targetType))
                {
                    return converter.ConvertTo(@this, targetType);
                }

                converter = TypeDescriptor.GetConverter(targetType);
                if (converter.CanConvertFrom(@this.GetType()))
                {
                    return converter.ConvertFrom(@this);
                }
                if (@this == DBNull.Value)
                {
                    return null;
                }
            }

            return @this;
        }
    }
}
